<?php

class address_detail extends a_admin_controller {

    protected $form_data;
    protected $form_errors;
    protected $srv;
    protected $address_id;
    protected $user_id;

    /*
     * @TODO: function get_groups_n_field() does not seem to
     * work when it is being called from index function, only from constructor.
     *
     * *edit: this currently may not need to change since the issue of receiving
     * a post has been handled. I am receiving the post from the function
     * get_groups_n_field()
     */
    public function __construct() {
        parent::__construct(true);
        $this->srv = new user_service_l_user();
        $this->form_data = array();

        $this->get_groups_n_field();
    }

    public function batch_assign(){
        $this->assign("form_data", $this->form_data);
        $this->assign("form_errors", $this->form_errors);
        $this->assign("user_id", $this->user_id);
    }

    public function index(){
        if($this->input->post('view'))
        {
        }
        else if($this->input->post('delete'))
        {
            $this->delete_address();
        }
    }

    protected function delete_address($address_id = NULL, $user_id = NULL)
    {
        $this->address_id = $address_id==NULL ? $this->input->post('address_id') : $address_id;
        $user_id = $user_id==NULL ? $this->input->post('id') : $user_id;

        $this->srv->delete_user_address($this->address_id);

        redirect(base_url()."index.php/user/admin/user_detail/load_user_details/".$user_id);
    }
    
    protected function edit_address()
    {
        $this->address_id = $this->input->post('address_id');
        $this->user_id = $this->input->post("user_id");
        
        if($this->form->valid){
            $vars = $this->form->get_post();

            $new_user_id = $this->srv->edit_address($vars);

            redirect(base_url()."index.php/user/admin/user_detail/load_user_details/".$this->user_id);
        }
        else{
            $this->form_errors = $this->form_errors."<br>".$this->form->errors;
        }


    }

    protected function get_groups_n_field() {
        $address = new addresses_m_user();
        $this->address_id = $this->input->post('address_id');
        $address = $address->get_by_id($this->address_id);
        $this->user_id = $address->user_id;

        $obj = new field_groups_m_user();

        if(strcmp($address->type, "S")==0)
        {
            $groups = $obj->where("visible","1")->where("group_name","shipping_address")->get_iterated();
            $this->form->hidden('type', "S");
        }
        else
        {
            $groups = $obj->where("visible","1")->where("group_name","billing_address")->get_iterated();
            $this->form->hidden('type', "B");
        }

        foreach ($groups as $group) {

            $this->form->fieldset($group->group_description);
            $fields = $group->reg_fields_m_user->order_by("order")->get();
            
            foreach ($fields as $field) {

                $field_name=$field->field_name;

                if(strcmp("S", $address->type)==0)
                {
                    $field_name = substr($field_name, 9);
                }
                else if(strcmp("B", $address->type)==0)
                {
                    $field_name = substr($field_name, 8);
                }

                $default_value = $address->$field_name;

                switch ($field->rendering_class) {
                    case "text":
                        $this->form->text($field->field_name, $field->field_description, $field->validation_rule, $default_value, array('title' => $field->field_note));
                        break;
                    case "textarea":
                        $this->form->textarea($field->field_name, $field->field_description, $field->validation_rule, $default_value, array('title' => $field->field_note));
                        break;
                    case "radiogroup":
                        $radios = array();
                        $pieces = explode(";", $field->values);
                        foreach ($pieces as $piece)
                            $radios[] = explode("=", $piece);
                        $this->form->radiogroup($field->field_name, $radios, $field->field_description, $default_value, $field->validation_rule, array('title' => $field->field_note));
                        break;
                    case "checkbox":
                        $this->form->br();
                        $this->form->checkbox($field->field_name, $field->values, $field->field_description,$field->default_value=="checked" ? TRUE: FALSE, $field->validation_rule, array('title' => $field->field_note));
                        break;
                    case "checkgroup":
                        $options = array();
                        $pieces = explode(";", $field->values);
                        foreach ($pieces as $piece)
                            $options[] = explode("=", $piece);
                        $this->form->checkgroup($field->field_name, $options, $field->field_description, $field->default_value, $field->validation_rule, array('title' => $field->field_note));
                        break;
                    case "select_one"    :
                        $options = array();
                        $pieces = explode(";", $field->values);
                        foreach ($pieces as $piece){
                            $even_more_pieces = explode("=", $piece);
                            $options[$even_more_pieces[0]] = $even_more_pieces[1];
                        }
                        $this->form->select($field->field_name, $options,  $field->field_description, $field->default_value,$field->validation_rule, array('title' => $field->field_note));
                        break;
                    default:
                        break;
                }
                if(strlen($field->field_error_msg)>0){
                    $this->form->set_error($field->field_name, $field->field_error_msg);
                }
            }
        }
        $this->form->indent(150);
        $this->form->hidden('user_id', $this->user_id);
        $this->form->hidden('address_id', $this->address_id);
        $this->form->hidden(i_handle_posts::OP_CODE,"edit_address");

        $this->form->submit("Save");
        $this->form_data[] = $this->form->get();
    }
}

?>